# encoding: utf-8
import sys, re

reload(sys)
sys.setdefaultencoding("utf-8")


def _add1(matched):
    """
    php应用版本第二位加1
    :param matched:
    :return:
    """
    _str = matched.group(4)
    return '%s%s%s%s%s%s' % (
        matched.group(1), matched.group(2), matched.group(3), int(_str) + 1, matched.group(5), matched.group(6))


def replacePhpVersion(path, v):
    file_object = open(path)
    _v = None
    try:
        text = file_object.read()
        _match = re.search("('{1})((\\d+\\.){1,})(\\d+)('{1})", text)
        if _match:
            # result = re.sub("(')(\\d+)(\\.)(\\d+)(\\.)(\\d+)", _add1, text)
            result = re.sub("('{1})((\\d+\\.){1,})(\\d+)('{1})", "'%s'" % v, text)
            _v = str(_match.group()).replace("'","")
            output = open(path, 'w')
            try:
                output.write(result)
            finally:
                output.close()
    finally:
        file_object.close()
    return _v

# def main():
#     file_object = open('/Users/wangjian/Desktop/version.php')
#     try:
#         all_the_text = file_object.read()
#         print all_the_text
#         aa = re.search("(')(\\d+)(\\.)(\\d+)(\\.)(\\d+)", all_the_text)
#         if aa:
#             new = int(aa.group(4)) + 1
#             print str(new)
#             result = re.sub("(')(\\d+)(\\.)(\\d+)(\\.)(\\d+)", _add1, all_the_text)
#             print result
#             output = open('/Users/wangjian/Desktop/version1.php', 'w')
#             output.write(result)
#             output.close()
#     finally:
#         file_object.close()


# if __name__ == '__main__':
#     print replacePhpVersion('/Users/wangjian/Desktop/version.php', '1.2.65')
